--#include "data\config\activityconfig\OpenServer\OpenServerDailyRechargeConfig.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理
--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once

function IsInOpenServerDailyRechargeDay()
	local combineday = System.getDaysSinceCombineServer()
	local openday = System.getDaysSinceOpenServer()
	if OpenServerDailyRechargeCfg.NotCondOpenDays then
		for idx = 1, #OpenServerDailyRechargeCfg.NotCondOpenDays do 
			if openday == OpenServerDailyRechargeCfg.NotCondOpenDays[idx] then
				return false
			end
		end
	end
	if combineday and combineday > 0 and OpenServerDailyRechargeCfg.NotCondCombineDays then
		for idx = 1, #OpenServerDailyRechargeCfg.NotCondCombineDays do 
			if combineday == OpenServerDailyRechargeCfg.NotCondCombineDays[idx] then
				return false
			end
		end
	end
	return true
end

function GetOpenServerDailyRechargeDay()
	local combineday = System.getDaysSinceCombineServer()
	local openday = System.getDaysSinceOpenServer()
	local needDeleteNum = 0
	if OpenServerDailyRechargeCfg.NotCondOpenDays then
		for idx = 1, #OpenServerDailyRechargeCfg.NotCondOpenDays do 
			if openday >= OpenServerDailyRechargeCfg.NotCondOpenDays[idx] then
				needDeleteNum = needDeleteNum + 1
			end
		end
	end
	if combineday and combineday > 0 and OpenServerDailyRechargeCfg.NotCondCombineDays then
		for idx = 1, #OpenServerDailyRechargeCfg.NotCondCombineDays do 
			if combineday >= OpenServerDailyRechargeCfg.NotCondCombineDays[idx] then
				needDeleteNum = needDeleteNum + 1
			end
		end
	end
	local day = openday - needDeleteNum
	if day <= 0 then
		day = 1
	end
	return day
end

--每日充值元宝
function OpenServerDailyRechargeWithDrawYuanBao(sysarg, Gold)
	if not IsInOpenServerDailyRechargeDay() then
		return
	end
	
	local cfg = OpenServerDailyRechargeCfg 
	if not cfg then
		return
	end
	local sVar = Actor.getStaticVar(sysarg)

	--if not sVar.firstRechargeState or sVar.firstRechargeState == 0 then		--第一次充值不计算
	--	return
	--end

	sVar.openServerDailyRechargeYb = (sVar.openServerDailyRechargeYb or 0) + Gold

	if not sVar.openServerDailyRechargeState then
		sVar.openServerDailyRechargeState = {}
	end
	
	for idx = 1, #cfg.Gold do
		if sVar.openServerDailyRechargeState[idx] and sVar.openServerDailyRechargeState[idx] > 1 then
		elseif cfg.Gold[idx] <= sVar.openServerDailyRechargeYb then
			sVar.openServerDailyRechargeState[idx] = 1	
		end
	end
	SendOpenServerDailyRechargeData(sysarg)
end

--跨天邮件玩家
function OpenServerDailyRechargeSendMail(sysarg)
	local cfg = OpenServerDailyRechargeCfg 
	if not cfg then
		return
	end
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.openServerDailyRechargeStateDay then
		return
	end
	if not sVar.openServerDailyRechargeState then
		sVar.openServerDailyRechargeState = {}
	end
	local rewardCfg = cfg.Rewards[sVar.openServerDailyRechargeStateDay]
	if not rewardCfg then
		rewardCfg = cfg.Rewards[#cfg.Rewards]	
	end
	local len = #cfg.Gold
	for idx = 1, len do
		local state = sVar.openServerDailyRechargeState[idx] or 0
		if state == 1 then
			local rewards = rewardCfg[idx]
			local title = Lang.ScriptTips.OpenServerDailyRecharge001
			local content = Lang.ScriptTips.OpenServerDailyRecharge002
			local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        	local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        	local actorId = Actor.getActorId(sysarg)
			Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OpenServerDailyRechargeLogId, Lang.LogTips.OpenServerDailyRecharge, vocation, sex)
			break
		end
	end
	sVar.openServerDailyRechargeYb = nil
	sVar.openServerDailyRechargeState = {}
	sVar.openServerDailyRechargeStateDay = GetOpenServerDailyRechargeDay()
	SendOpenServerDailyRechargeData(sysarg)
end

--发送每日充值数据
function SendOpenServerDailyRechargeData(sysarg)
	if not IsInOpenServerDailyRechargeDay() then
		return
	end
	
	local cfg = OpenServerDailyRechargeCfg 
	if not cfg then
		return
	end

	local sVar = Actor.getStaticVar(sysarg)


	local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerDailyRechargeInfo)
	if (pack == nil) then
		return
	end
	if not sVar.openServerDailyRechargeStateDay then
		sVar.openServerDailyRechargeStateDay = GetOpenServerDailyRechargeDay()
	end
	if not sVar.openServerDailyRechargeState then
		sVar.openServerDailyRechargeState = {}
	end
	DataPack.writeByte(pack, sVar.openServerDailyRechargeStateDay)
	local len = #cfg.Gold
	DataPack.writeByte(pack, len)
	for idx = 1, len do
		local state = sVar.openServerDailyRechargeState[idx] or 0
		DataPack.writeByte(pack, idx)
		DataPack.writeByte(pack, state)
	end
	DataPack.writeInt(pack, sVar.openServerDailyRechargeYb or 0)
	DataPack.flush(pack)
end

--领取奖励
function GetOpenServerDailyRechargeAwards(sysarg, packet)
	if not IsInOpenServerDailyRechargeDay() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyRecharge004, ttFlyTip)
		return
	end
	local Index = DataPack.readByte(packet)
	local cfg = OpenServerDailyRechargeCfg
	if not cfg then
		return
	end
	if Index > #cfg.Gold then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyRecharge005, ttFlyTip)
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	
	if not sVar.openServerDailyRechargeState then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyRecharge006, ttFlyTip)
		return
	end
	if not sVar.openServerDailyRechargeState[Index] or sVar.openServerDailyRechargeState[Index] ~= 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyRecharge006, ttFlyTip)
		return
	end

	if not sVar.openServerDailyRechargeStateDay then
		sVar.openServerDailyRechargeStateDay = GetOpenServerDailyRechargeDay()
	end

	local rewardCfg = cfg.Rewards[sVar.openServerDailyRechargeStateDay]
	if not rewardCfg then
		rewardCfg = cfg.Rewards[#cfg.Rewards]	
	end

	local rewards = rewardCfg[Index]

	sVar.openServerDailyRechargeState[Index] = 2
	
	if IsBagGridEnough(sysarg, rewards) then
		GiveCommonAward(sysarg, rewards, LogIds.OpenServerDailyRechargeLogId, Lang.LogTips.OpenServerDailyRecharge)
	else
		local actorId = Actor.getActorId(sysarg)
		local title = Lang.ScriptTips.OpenServerDailyRecharge001
		local content = Lang.ScriptTips.OpenServerDailyRecharge002
		local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
		Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OpenServerDailyRechargeLogId, Lang.LogTips.OpenServerDailyRecharge, vocation, sex)
	end

	SendOpenServerDailyRechargeData(sysarg)
end